//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "HullWhiteProcess.h"
using namespace Cephei::QL::Processes;
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/StochasticProcess1D.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Processes::CHullWhiteProcess::CHullWhiteProcess (Cephei::QL::Termstructures::IYieldTermStructure^ h, Double a, Double sigma) : CStochasticProcess1D(CHullWhiteProcess::typeid)
{
    CYieldTermStructure^ _Ch;
    try
    {
#ifdef HANDLE
        _phHullWhiteProcess = NULL;
#endif
        _Ch = safe_cast<CYieldTermStructure^> (h);
        _Ch->Lock();
        Handle<QuantLib::YieldTermStructure>& _h = static_cast<Handle<QuantLib::YieldTermStructure>&> (_Ch->GetHandle ()); 
        QuantLib::Real _a = (QuantLib::Real)ValueHelper::Convert (a);
        QuantLib::Real _sigma = (QuantLib::Real)ValueHelper::Convert (sigma);
        _ppHullWhiteProcess = new boost::shared_ptr<QuantLib::HullWhiteProcess> (new QuantLib::HullWhiteProcess ( _h,  _a,  _sigma ));
        SetStochasticProcess1D (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppHullWhiteProcess));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ch != nullptr) _Ch->Unlock();
    }
}
Cephei::QL::Processes::CHullWhiteProcess::CHullWhiteProcess (boost::shared_ptr<QuantLib::HullWhiteProcess>& childNative, Object^ owner) : CStochasticProcess1D(CHullWhiteProcess::typeid)
{
#ifdef HANDLE
	_phHullWhiteProcess = NULL;
#endif
	_ppHullWhiteProcess = &childNative;
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppHullWhiteProcess));
}
Cephei::QL::Processes::CHullWhiteProcess::CHullWhiteProcess (QuantLib::HullWhiteProcess& childNative, Object^ owner) : CStochasticProcess1D(CHullWhiteProcess::typeid)
{
#ifdef HANDLE
	_phHullWhiteProcess = NULL;
#endif
	_ppHullWhiteProcess = new boost::shared_ptr<QuantLib::HullWhiteProcess> (&childNative);
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppHullWhiteProcess));
    _HullWhiteProcessOwner = owner;
    _StochasticProcess1DOwner = owner;
}

Cephei::QL::Processes::CHullWhiteProcess::CHullWhiteProcess (CHullWhiteProcess^ copy) : CStochasticProcess1D(CHullWhiteProcess::typeid)
{
#ifdef HANDLE
	_phHullWhiteProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppHullWhiteProcess = new boost::shared_ptr<QuantLib::HullWhiteProcess> (copy->GetShared());
        _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppHullWhiteProcess));
    }
}
Cephei::QL::Processes::CHullWhiteProcess::CHullWhiteProcess (System::Type^ t) : CStochasticProcess1D(CHullWhiteProcess::typeid)
{
#ifdef HANDLE
	_phHullWhiteProcess = NULL;
#endif
	if (!t->IsSubclassOf(CHullWhiteProcess::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Processes::CHullWhiteProcess::CHullWhiteProcess (QuantLib::Handle<QuantLib::HullWhiteProcess>& childNative, Object^ owner)  : CStochasticProcess1D(CHullWhiteProcess::typeid)
{
	_phHullWhiteProcess = &childNative;
	_ppHullWhiteProcess = &static_cast<boost::shared_ptr<QuantLib::HullWhiteProcess>>(childNative.currentLink());
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppHullWhiteProcess));
    _HullWhiteProcessOwner = owner;
}
Cephei::QL::Processes::CHullWhiteProcess::CHullWhiteProcess (QuantLib::Handle<QuantLib::HullWhiteProcess> childNative)  : CStochasticProcess1D(CHullWhiteProcess::typeid)
{
	_phHullWhiteProcess = &childNative;
	_ppHullWhiteProcess = &static_cast<boost::shared_ptr<QuantLib::HullWhiteProcess>>(childNative.currentLink());
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppHullWhiteProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Processes::CHullWhiteProcess::CHullWhiteProcess (QuantLib::HullWhiteProcess childNative)  : CStochasticProcess1D(CHullWhiteProcess::typeid)
{
#ifdef HANDLE
	_phHullWhiteProcess = NULL;
#endif
	_ppHullWhiteProcess = new boost::shared_ptr<QuantLib::HullWhiteProcess> (new QuantLib::HullWhiteProcess (childNative));
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppHullWhiteProcess));
}
#endif

Cephei::QL::Processes::CHullWhiteProcess::~CHullWhiteProcess ()
{
    if (_ppHullWhiteProcess != NULL)
    {
	    delete _ppHullWhiteProcess;
        _ppHullWhiteProcess = NULL;
    }
}
Cephei::QL::Processes::CHullWhiteProcess::!CHullWhiteProcess ()
{
    if (_ppHullWhiteProcess != NULL)
    {
	    delete _ppHullWhiteProcess;
    }
}
QuantLib::HullWhiteProcess& Cephei::QL::Processes::CHullWhiteProcess::GetReference ()
{
    if (_ppHullWhiteProcess == NULL) throw gcnew NativeNullException ();
	return **_ppHullWhiteProcess;
}
boost::shared_ptr<QuantLib::HullWhiteProcess>& Cephei::QL::Processes::CHullWhiteProcess::GetShared ()
{
    if (_ppHullWhiteProcess == NULL) throw gcnew NativeNullException ();
	return *_ppHullWhiteProcess;
}
QuantLib::HullWhiteProcess* Cephei::QL::Processes::CHullWhiteProcess::GetPointer ()
{
    if (_ppHullWhiteProcess == NULL) throw gcnew NativeNullException ();
	return &**_ppHullWhiteProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::HullWhiteProcess>& Cephei::QL::Processes::CHullWhiteProcess::GetHandle ()
{
	if (_phHullWhiteProcess == NULL)
	{
		_phHullWhiteProcess = new Handle<QuantLib::HullWhiteProcess> (*_ppHullWhiteProcess);
	}
	return *_phHullWhiteProcess;
}
#endif
bool Cephei::QL::Processes::CHullWhiteProcess::HasNative () 
{
	return (_ppHullWhiteProcess != NULL);
}

Double Cephei::QL::Processes::CHullWhiteProcess::A::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteProcess)->a ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteProcess::Alpha (Double t)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteProcess)->alpha ( _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteProcess::Diffusion (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteProcess)->diffusion ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteProcess::Drift (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteProcess)->drift ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteProcess::Expectation (Double t0, Double x0, Double dt)
{
    try
    {
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0);
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0);
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteProcess)->expectation ( _t0,  _x0,  _dt );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteProcess::Sigma::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteProcess)->sigma ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteProcess::StdDeviation (Double t0, Double x0, Double dt)
{
    try
    {
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0);
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0);
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteProcess)->stdDeviation ( _t0,  _x0,  _dt );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteProcess::Variance (Double t0, Double x0, Double dt)
{
    try
    {
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0);
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0);
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteProcess)->variance ( _t0,  _x0,  _dt );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteProcess::X0::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteProcess)->x0 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Processes::IHullWhiteProcess^ Cephei::QL::Processes::CHullWhiteProcess_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ h, Double a, Double sigma)
{
    return gcnew CHullWhiteProcess ( h,  a,  sigma);
}
